{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 91,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline\n",
    "from sklearn.datasets.samples_generator import make_classification\n",
    "import tensorflow as tf\n",
    "tf.reset_default_graph()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1000,)\n",
      "<built-in method astype of numpy.ndarray object at 0x2b2bc3f80e90>\n",
      "<built-in method astype of numpy.ndarray object at 0x2b2bc3ec9670>\n"
     ]
    }
   ],
   "source": [
    "### 生成数据集 1000个1维样本   正负各半   0-10 label为1   -10--1 label为0\n",
    "xa = np.random.randint(0,10,size=500)\n",
    "xb = np.random.randint(-10,-1,size=500)\n",
    "xa = xa.tolist()\n",
    "xb = xb.tolist()\n",
    "xa.extend(xb)\n",
    "X1 = np.array(xa)\n",
    "\n",
    "y = [1]*500\n",
    "yb = [0]*500\n",
    "\n",
    "y.extend(yb)\n",
    "y1 = np.array(y)\n",
    "\n",
    "print(X1.shape)\n",
    "\n",
    "\n",
    "state = np.random.get_state()\n",
    "np.random.shuffle(X1)\n",
    "np.random.set_state(state)\n",
    "np.random.shuffle(y1)\n",
    "\n",
    "X1 = X1.reshape(-1,1)\n",
    "\n",
    "# print(X1)\n",
    "print(X1.astype)\n",
    "print(y1.astype)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "metadata": {},
   "outputs": [],
   "source": [
    "### 参数设置\n",
    "learning_rate = 0.01\n",
    "training_epochs = 10\n",
    "batch_size = 10\n",
    "\n",
    "x = tf.placeholder(tf.float32, [None, 1],name='inputx') # 6 features\n",
    "y = tf.placeholder(tf.float32, [None, 2]) # 3 classes\n",
    "\n",
    "W = tf.Variable(tf.zeros([1, 2]))\n",
    "b = tf.Variable(tf.zeros([2]))\n",
    "\n",
    "# softmax回归\n",
    "pred = tf.nn.softmax(tf.matmul(x, W) + b, name=\"softmax\") \n",
    "cost = tf.reduce_mean(-tf.reduce_sum(y*tf.log(pred), reduction_indices=1))\n",
    "optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)\n",
    "\n",
    "prediction_labels = tf.argmax(pred, axis=1, name=\"output\")\n",
    "\n",
    "init = tf.global_variables_initializer()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "inputx_1:0\n"
     ]
    }
   ],
   "source": [
    "### 查看变量名\n",
    "print(x.name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 1.  0.]\n",
      " [ 0.  1.]\n",
      " [ 0.  1.]\n",
      " [ 1.  0.]\n",
      " [ 0.  1.]\n",
      " [ 1.  0.]\n",
      " [ 0.  1.]\n",
      " [ 1.  0.]\n",
      " [ 1.  0.]\n",
      " [ 1.  0.]]\n"
     ]
    }
   ],
   "source": [
    "### label onhot\n",
    "sess = tf.Session()\n",
    "sess.run(init)\n",
    "y2 = tf.one_hot(y1, 2)\n",
    "y2 = sess.run(y2)\n",
    "\n",
    "print(y2[:10])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0.52383691  0.47616306]\n",
      " [ 0.45243427  0.54756576]\n",
      " [ 0.52383691  0.47616306]\n",
      " [ 0.59427822  0.40572172]\n",
      " [ 0.45243427  0.54756576]\n",
      " [ 0.47616306  0.52383691]\n",
      " [ 0.5         0.5       ]\n",
      " [ 0.48807478  0.51192528]\n",
      " [ 0.53572154  0.46427846]\n",
      " [ 0.58272505  0.41727498]] [[ 1.  0.]\n",
      " [ 0.  1.]\n",
      " [ 1.  0.]\n",
      " [ 1.  0.]\n",
      " [ 0.  1.]\n",
      " [ 0.  1.]\n",
      " [ 0.  1.]\n",
      " [ 0.  1.]\n",
      " [ 1.  0.]\n",
      " [ 1.  0.]]\n",
      "Epoch: 0002 cost= 0.588380873\n",
      "[[ 0.56066394  0.43933609]\n",
      " [ 0.38024566  0.61975431]\n",
      " [ 0.56066394  0.43933609]\n",
      " [ 0.7263574   0.27364257]\n",
      " [ 0.38024566  0.61975431]\n",
      " [ 0.43920606  0.56079394]\n",
      " [ 0.49993402  0.50006598]\n",
      " [ 0.469457    0.53054303]\n",
      " [ 0.5904721   0.4095279 ]\n",
      " [ 0.70143884  0.29856113]] [[ 1.  0.]\n",
      " [ 0.  1.]\n",
      " [ 1.  0.]\n",
      " [ 1.  0.]\n",
      " [ 0.  1.]\n",
      " [ 0.  1.]\n",
      " [ 0.  1.]\n",
      " [ 0.  1.]\n",
      " [ 1.  0.]\n",
      " [ 1.  0.]]\n",
      "Epoch: 0004 cost= 0.459417999\n",
      "[[ 0.58764702  0.41235301]\n",
      " [ 0.32944965  0.67055035]\n",
      " [ 0.58764702  0.41235301]\n",
      " [ 0.80520803  0.19479196]\n",
      " [ 0.32944965  0.67055035]\n",
      " [ 0.41200054  0.5879994 ]\n",
      " [ 0.49981818  0.50018185]\n",
      " [ 0.45556396  0.54443604]\n",
      " [ 0.62988734  0.37011266]\n",
      " [ 0.77585661  0.22414346]] [[ 1.  0.]\n",
      " [ 0.  1.]\n",
      " [ 1.  0.]\n",
      " [ 1.  0.]\n",
      " [ 0.  1.]\n",
      " [ 0.  1.]\n",
      " [ 0.  1.]\n",
      " [ 0.  1.]\n",
      " [ 1.  0.]\n",
      " [ 1.  0.]]\n",
      "Epoch: 0006 cost= 0.386892080\n",
      "[[ 0.60855484  0.39144522]\n",
      " [ 0.29187131  0.70812863]\n",
      " [ 0.60855484  0.39144522]\n",
      " [ 0.85430777  0.14569217]\n",
      " [ 0.29187131  0.70812863]\n",
      " [ 0.39083713  0.60916287]\n",
      " [ 0.49968082  0.50031912]\n",
      " [ 0.44459453  0.55540544]\n",
      " [ 0.65982187  0.34017813]\n",
      " [ 0.82455772  0.17544234]] [[ 1.  0.]\n",
      " [ 0.  1.]\n",
      " [ 1.  0.]\n",
      " [ 1.  0.]\n",
      " [ 0.  1.]\n",
      " [ 0.  1.]\n",
      " [ 0.  1.]\n",
      " [ 0.  1.]\n",
      " [ 1.  0.]\n",
      " [ 1.  0.]]\n",
      "Epoch: 0008 cost= 0.341186345\n",
      "[[ 0.62550771  0.37449229]\n",
      " [ 0.26278144  0.73721856]\n",
      " [ 0.62550771  0.37449229]\n",
      " [ 0.8867079   0.11329205]\n",
      " [ 0.26278144  0.73721856]\n",
      " [ 0.37362179  0.62637818]\n",
      " [ 0.49953529  0.50046474]\n",
      " [ 0.43553957  0.56446046]\n",
      " [ 0.68361133  0.31638867]\n",
      " [ 0.85816365  0.14183635]] [[ 1.  0.]\n",
      " [ 0.  1.]\n",
      " [ 1.  0.]\n",
      " [ 1.  0.]\n",
      " [ 0.  1.]\n",
      " [ 0.  1.]\n",
      " [ 0.  1.]\n",
      " [ 0.  1.]\n",
      " [ 1.  0.]\n",
      " [ 1.  0.]]\n",
      "Epoch: 0010 cost= 0.309734344\n",
      "优化完毕!\n",
      "0.961\n"
     ]
    }
   ],
   "source": [
    "### 训练\n",
    "for epoch in range(training_epochs):\n",
    "    \n",
    "    _, c, pre = sess.run([optimizer, cost, pred], feed_dict={x: X1, y: y2})\n",
    "    if (epoch+1) % 2 == 0:\n",
    "        print(pre[:10], y2[:10])\n",
    "        print (\"Epoch:\", '%04d' % (epoch+1), \"cost=\", \"{:.9f}\".format(c))\n",
    "    \n",
    "print (\"优化完毕!\")\n",
    "correct_prediction = tf.equal(tf.argmax(pred, 1), tf.argmax(y2, 1))\n",
    "accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))\n",
    "acc = sess.run(accuracy, feed_dict={x: X1, y: y2})\n",
    "print (acc)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "INFO:tensorflow:Froze 2 variables.\n",
      "Converted 2 variables to const ops.\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'./0631demo_rf.pb'"
      ]
     },
     "execution_count": 81,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "### 模型存储为pb格式\n",
    "\n",
    "graph = tf.graph_util.convert_variables_to_constants(sess, sess.graph_def, [\"inputx\",\"output\"])\n",
    "tf.train.write_graph(graph, '.', '0631demo_rf.pb', as_text=False)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
